import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/bloc/ContentSearchResultCubit.dart';
import 'package:im_app/im/page/view/ListSeparator.dart';
import 'package:im_app/im/page/view/LoadingView.dart';
import 'package:im_app/im/page/view/ContentSearchView.dart';
import 'package:im_app/im/page/view/component/SessionIconView.dart';
import 'package:im_app/im/page/view/component/UserIconView.dart';
import 'package:im_app/im/util/ImConstant.dart';

import '../../pojo/ImPojo.dart';
import '../bloc/SessionListCubit.dart';

/// 消息汇总列表： 会话中能够查询出消息个数
class ContentSearchResultPage extends StatelessWidget {
  final String contentSearch;
  final ImSession session;

  ContentSearchResultPage({required this.contentSearch, required this.session});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(create: (context)=>MessageSearchCubit()..search(session.id!, contentSearch),
        child: _ContentSearchResultPage(session: session, contentSearch: contentSearch,));
  }
}

class _ContentSearchResultPage extends StatefulWidget {
  final String contentSearch;
  final ImSession session;

  _ContentSearchResultPage({required this.contentSearch, required this.session});

  @override
  State<StatefulWidget> createState() => _ContentSearchResultState();
}

class _ContentSearchResultState extends State<_ContentSearchResultPage> {

  final controller = TextEditingController();
  String _searchText = '';

  @override
  void initState() {
    super.initState();
    setState(() {
      _searchText = widget.contentSearch;
    });
  }

  @override
  Widget build(BuildContext context) {
    var messageSearchCubit = context.watch<MessageSearchCubit>();
    return Scaffold(
        body: SafeArea(
          left: false, right: false,
          child: Column(
            children: [
              Padding(padding: const EdgeInsets.only(left: 5, right: 5, top: 10), child: ContentSearchView(searchHandler:
                _doSearch, prefix: 'demo', defaultText: _searchText,),),
                _buildSessionTitle(context),
              Container(height: 5, color: Colors.black12),
              Expanded(child: LoadingView(state: messageSearchCubit.state.state, child: _buildMessageListPane(context))),
            ],
          ),
        )
    );
  }

  Widget _buildSessionTitle(BuildContext context) {
    return ListTile(
      leading: SessionIconView(session: widget.session, iconSize: ImConstant.IM_USER_ICON_SIZE - 10,),
      title: Text(widget.session?.name??''),
      trailing: IconButton(icon: const Icon(Icons.arrow_right),
        onPressed: () {
          var sessionListCubit = context.read<SessionListCubit>();
          sessionListCubit.addSession(widget.session);
          Navigator.of(context).pushNamedAndRemoveUntil("/MessageSendPage", ModalRoute.withName('/MainPage'), arguments: {"id": widget.session?.id!, "name": widget.session?.name});
        },)
    );
  }

  Widget _buildMessageListPane(BuildContext context) {
    var messageSearchCubit = context.read<MessageSearchCubit>();
    return ListView.separated(
      itemBuilder: (BuildContext context, int index)=>
       InkWell(child: _buildListTitle(messageSearchCubit.state.messages[index]),
        onTap: ()=>_showMessageList(messageSearchCubit.state.messages[index]),
       ),
      itemCount: messageSearchCubit.state.messages.length,
      separatorBuilder: (BuildContext context, int index) {
        return ListSeparator();
      },
    );
  }

  Widget _buildListTitle(ImMessage message) {
    return ListTile(
      leading: UserIconView(user: message.fromImUser!, iconSize: ImConstant.IM_USER_ICON_SIZE - 10,),
      title: Text(message.content),
    );
  }

  void onSearchTextChanged(String text) {
    setState(() {
      this._searchText = text;
    });
  }

  void _doSearch(BuildContext context, String search) {

  }

  void _showMessageList(ImMessage message) async {
    // Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {
    //   var messageListCubit = MessageListCubit(sessionId: widget.session.id!);
    //   return MultiBlocProvider(providers: [
    //     BlocProvider(
    //       create: (_) => messageListCubit,
    //     ),
    //     BlocProvider(
    //       create: (BuildContext context) => MessageSendCubit(messageListCubit: messageListCubit, sessionId: widget.session.id!),
    //     )
    //   ], child: MessageListPage(contentSearch: _searchText, sessionId: widget.session.id!, message: message));
    // }));

    Navigator.of(context).pushNamedAndRemoveUntil("/MessageSendPage", ModalRoute.withName('/MainPage'),
        arguments: {"id": message?.sessionId!, "name": widget.session.name, "messageId": message?.id});
  }

}
